package com.yj.action;

import java.util.Calendar;
import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yj.model.Course;
import com.yj.model.Teacher;
import com.yj.utils.DateUtil;

@Controller("teacherAction")
@Scope("prototype")
public class TeacherAction extends BaseAction<Teacher> {

	private static final long serialVersionUID = 1L;

	public String checkAll() {
		if(session.get("teacher") == null) {
			return "login";
		}
		return "checkAll";
	}
	
	public String checkCourse() {
		if(session.get("teacher") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer cid = Integer.parseInt(hsRequest.getParameter("cid"));
		Course course = courseService.get(cid);	
		request.put("course", course);
		return "checkCourse";
	}
	
	public String addCourse() {
		if(session.get("teacher") == null) {
			return "login";
		}
		return "addCourse";
	}
	
	public String insertCourse() {
		if(session.get("teacher") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
	    String name = hsRequest.getParameter("name");
	    String type = hsRequest.getParameter("type");
	    Integer score = Integer.parseInt(hsRequest.getParameter("score"));
	    Integer sum = Integer.parseInt(hsRequest.getParameter("sum"));
	    
	    Calendar now = Calendar.getInstance();
	    int year = now.get(Calendar.YEAR);
	    int month = (now.get(Calendar.MONTH) + 1);
		if(month > 1 && month < 7){
		    month = 6;
		}else if((month > 6 && month < 13) || (month > 0 || month < 2)){
		    month = 2;
		}
	    String startTime = year + "年0" + month + "月01日 " + "20:00:00";
	    String endTime = year + "年0" + month + "月02日 " + "20:00:00";
	    Course course = new Course();
	    course.setTeacher((Teacher)session.get("teacher"));
	    course.setName(name);
	    course.setType(type);
	    course.setScore(score);
	    course.setSum(sum);
	    course.setSelected(0);
	    try {
			course.setStartTime(DateUtil.toLong(startTime));
			course.setEndTime(DateUtil.toLong(endTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	    courseService.save(course);
	    Teacher teacher = (Teacher) session.get("teacher");
	    session.put("teacher", userService.login(teacher, 2));
	    request.put("message", "课程" + name + "添加成功");
		return "insertCourse";
	}
	
	public String dropCourse() {
		if(session.get("teacher") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer sid = Integer.parseInt(hsRequest.getParameter("sid"));
		Integer cid = Integer.parseInt(hsRequest.getParameter("cid"));
		scService.delete(sid);
		Course course = courseService.getById(cid);
	    course.setSelected(course.getSelected() - 1);
	    courseService.update(course);
	    Teacher teacher = (Teacher) session.get("teacher");
	    session.put("teacher", userService.login(teacher, 2));
		course = courseService.get(cid);	
		request.put("course", course);
	    return "dropCourse";
	}
}
